<?php

namespace Hilaoyu\LaravelExtend\Services\Files\Console;


use Carbon\Carbon;
use Hilaoyu\LaravelExtend\Services\Files\FileService;
use Hilaoyu\LaravelExtend\Supports\Cli;
use Hilaoyu\Utils\UtilCli;
use Hilaoyu\Utils\UtilFile;
use Hilaoyu\Utils\UtilSupport;
use Illuminate\Console\Command;
use Illuminate\Filesystem\FilesystemServiceProvider;
use Illuminate\Support\Facades\Storage;

class RunFileUseQueueCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'laravelExtend:runFileUseQueue';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Laravel extend files run fileuse queue';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $run_queue_command = Cli::buildArtisanCommand('laravelExtend:runFileUseQueue');
        if(UtilCli::hasRunning($run_queue_command,2)){
            return 0;
        }

        $queue = FileService::fileUseQueue();
        do{
            if($task = $queue->pop()){
                $disk = data_get($task,'disk');
                $path = data_get($task,'path');
                $file_model_id = data_get($task,'file_model_id');
                $secret = data_get($task,'secret');
                $config_disks = data_get($task,'config_filesystems_disks');

                if($config_disks){
                    //$app = app();
                    //$app['config']->set('filesystems.disks',$config_disks);
                    //( new FilesystemServiceProvider($app))->register();
                    config()->set('filesystems.disks',$config_disks);
                    UtilSupport::tryQuiet(function (){
                        foreach (config('filesystems.disks',[]) as $disk=>$dc){
                            app('filesystem')->forgetDisk($disk);
                        }
                    });
                    
                }
                //file_put_contents(storage_path("file_use_queue_".config('app.env','').'.txt'),"========queue handle file_model_id:".$file_model_id ." ======== \n",FILE_APPEND);
                //file_put_contents(storage_path("file_use_queue_".config('app.env','').'.txt'),"queue handle disK:".$disk." file_model_config_disks:".json_encode($config_disks)." config_disks:".json_encode(config("filesystems.disks"))."\n",FILE_APPEND);
                $filesModel = null;
                if($file_model_id){
                    $filesModel = FileService::loadFilesModel($file_model_id);
                }

                if($secret){
                    $secret = @base64_decode($secret);
                }

                if($FS = new FileService($disk,$path,$filesModel)){
                    $FS->useFor($secret);
                }
            }
            else{
                break;
            }

        }while(true);
        sleep(30);
        $output = null;
        Cli::execute('laravelExtend:runFileUseQueue',$output,'',false);
        return 0;
    }

}
